import { v4 as uuidv4 } from 'uuid'

const TOKEN_NAME = "USER_TOKEN" //用户登录后token
const TEMP_TOKEN_NAME = "TEMP_TOKEN" //用户未登录临时token

//存储登录成功的服务器返回的token
export const setUserToken = (token) => window.localStorage.setItem(TOKEN_NAME, token)
//从本地获取存储的token
export const getUserToken = () => window.localStorage.getItem(TOKEN_NAME)
//从本地删除存储的token
export const removeUserToken = () => window.localStorage.removeItem(TOKEN_NAME)


export const getTempToken = () => {
  const userToken = window.localStorage.getItem(TOKEN_NAME)
  if (!userToken) {
    let tempToken = window.localStorage.getItem(TEMP_TOKEN_NAME)
    if (!tempToken) {
      tempToken = uuidv4()
      window.localStorage.setItem(TEMP_TOKEN_NAME, tempToken)
    }
    return tempToken
  }
}